<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="{{ url_for('static',filename='css/bootstrap.min.css') }}" rel="stylesheet">
    <link href="{{ url_for('static',filename='css/docs.css') }}" rel="stylesheet">
    <link href="{{ url_for('static',filename='css/sweetalert2.min.css') }}" rel="stylesheet">

    <script src="{{ url_for('static',filename='js/bootstrap.bundle.min.js') }}"></script>
    <script src="{{ url_for('static',filename='js/crypto-js.min.js') }}"></script>
    <script src="{{ url_for('static',filename='js/jquery-1.12.4.min.js') }}"></script>
    <script src="{{ url_for('static',filename='js/sweetalert2.all.min.js') }}"></script>
    <script src="{{ url_for('static',filename='js/chart.js') }}"></script>
    <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>

    <link rel="icon" href="{{ url_for('static',filename='images/ico/favicon2.ico') }}">
    <title>账户设置</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <div class="container-fluid">
        <a class="navbar-brand">
            <img src="{{ url_for('static',filename='images/ico/favicon2.ico') }}" alt="" width="30" height="24">
            缘茶智学管理端-账号管理
        </a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent"
                aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarSupportedContent">

            <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                <li class="nav-item">
                    <a class="nav-link active" aria-current="page" href="/system">主页</a>
                </li>

                <!--上课模块开始-->
                <li class="nav-item">
                    <a class="nav-link active" href="/class">课程管理</a>
                </li>
                <!--上课模块结束-->


                <!--学生模块开始-->
                <!-- <li class="nav-item dropdown">
                  <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown"
                    aria-expanded="false">
                    学生状态
                  </a>
                  <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
                    <li><a class="dropdown-item" href="UndoScore">撤分审批</a></li>
                    <li><a class="dropdown-item" href="dutyMessage">值周信息</a></li> -->
                <!-- </ul>
                </li> -->
                <!-- 学生模块结束 -->

                <!--学生模块开始-->
                {#          <li class="nav-item">#}
                {#            <a class="nav-link active" aria-current="page" href="StuCdt">学生状态数据</a>#}
                {#          </li>#}
                <!--帮助模块结束-->


                <!--个人数据-->
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button"
                       data-bs-toggle="dropdown"
                       aria-expanded="false">
                        账号管理
                    </a>
                    <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
                        <li><a class="dropdown-item disabled">用户名：admin</a></li>
                        <li>
                            <hr class="dropdown-divider">
                        </li>
                        <li>
                            <a class="dropdown-item disabled">->账户设置</a>
                        </li>
                        <li>
                            <a class="dropdown-item" href="/systemlogout">登出</a>
                        </li>

                    </ul>
                </li>


            </ul>


            {#        <a class="btn btn-outline-success" type="submit" href="systemlogout">登出</a>#}

        </div>
    </div>
</nav>
<div class="container">
    <div class="card ">
        <div class="card-header">
            修改密码
        </div>

        <div class="card-body">
            <!-- 旧密码 -->
            <div class="input-group">
                <div class="input-group-text">
                    <label class="form-check-label">旧密码</label>
                </div>
                <label for="old_pwd"></label><input type="password" class="form-control" id="old_pwd" placeholder="密码">
            </div>
            <!-- 旧密码 -->
            <br>
            <!-- 新密码 -->
            <div class="input-group">
                <div class="input-group-text">
                    <label class="form-check-label">新密码</label>
                </div>
                <label for="new_pwd"></label><input type="password" class="form-control" id="new_pwd" placeholder="密码">
            </div>
            <!-- 新密码 -->
            <!-- 再次输入新密码 -->
            <div class="input-group">
                <div class="input-group-text">
                    <label class="form-check-label">再次输入新密码</label>
                </div>
                <label for="re_new_pwd"></label><input type="password" class="form-control" id="re_new_pwd" placeholder="密码">
            </div>
            <!-- 再次输入新密码 -->
            <br>
            <div align="center">
                <button class="btn btn-primary" onclick="change_pwd()">修改</button>
                <button class="btn btn-danger" onclick="clear_inputs()">清空</button>
            </div>
        </div>
    </div>
</div>
</body>

<script>
    function clear_inputs() {
        document.getElementById('old_pwd').value = '';
        document.getElementById('new_pwd').value = '';
        document.getElementById('re_new_pwd').value = '';
    }
    function change_pwd() {
        const xhr = new XMLHttpRequest();
        const old_pwd = document.getElementById("old_pwd").value;
        const new_pwd = document.getElementById("new_pwd").value;
        const re_new_pwd = document.getElementById("re_new_pwd").value;

        if (new_pwd !== re_new_pwd) {
            Swal.fire('两次输入密码不匹配', '', 'error')
                .then((_) => {
                    clear_inputs();
                })
            return
        }
        if (new_pwd === '') {
            Swal.fire('密码不得为空', '', 'error')
            return;
        }

        xhr.open('POST', '/account_management', true);
                xhr.setRequestHeader('Content-Type', 'application/json');
        const requestbody = {
            'old_pwd': CryptoJS.MD5(old_pwd).toString(),
            'new_pwd': CryptoJS.MD5(new_pwd).toString()
        };
        xhr.send(JSON.stringify(requestbody));

        //接受
        xhr.onreadystatechange = function () {
            if (xhr.readyState === XMLHttpRequest.DONE) {
                if (xhr.responseText === 'success') {
                    Swal.fire('密码修改成功', '', 'success')
                        .then((_) => {
                            clear_inputs();
                        })
                }
                else if (xhr.responseText === 'error') {
                    Swal.fire('原密码错误', '', 'error')
                        .then((_) => {
                            clear_inputs();
                        })
                }
                else {
                    Swal.fire('未知错误', '', 'error')
                        .then((_) => {
                            clear_inputs();
                        })
                }
            }
        }
    }
</script>